package b2.widget
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.util.*;
	import b2.widget.store.*;
	import b2.widget.tech.*;
	import b2.widget.warehouse.*;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	import flash.ui.Mouse;

	public class TankMessage extends Sprite
	{
		private var tankId:int;
		
		public function TankMessage(tankId:int)
		{
			super();
			this.tankId = tankId;
			var tankInfo:TankInfo = PlayerInfoManager.getInstance().tanks[tankId];
			var tankConfig:TankConfig = Config.objectConfigs[tankInfo.index];
			var roundRecContainer:Sprite = new Sprite();
			//坦克名字
			var tankTitle:TextField = new TextField();
			tankTitle.selectable = false;
        	tankTitle.autoSize = TextFieldAutoSize.LEFT;
			tankTitle.defaultTextFormat = new TextFormat("微软雅黑", 15, 0xFFFF00);
			tankTitle.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			var basicLevelName:String = Config.basicConfig.tankBasicLevel[tankInfo.basicLevel];
			var strengthenLevelName:String = Config.basicConfig.tankStrengthenLevel[tankInfo.strengthenLevel];
			tankTitle.text = tankConfig.name + " · " + basicLevelName + strengthenLevelName;
			roundRecContainer.addChild(tankTitle);
			//总战力
			var fpSprite:Sprite = getFightPoint(tankInfo.fightPoint);
			fpSprite.x = 230 - fpSprite.width;
			fpSprite.y = 3;
			roundRecContainer.addChild(fpSprite);
			//属性
			var allProperty:TextField = new TextField();
			allProperty.selectable = false;
        	allProperty.autoSize = TextFieldAutoSize.LEFT;
			allProperty.defaultTextFormat = new TextFormat("微软雅黑", 12, 0xFFFFFF);
			allProperty.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			allProperty.text = "总属性";
			allProperty.x = 0;
			allProperty.y = 30;
			roundRecContainer.addChild(allProperty);
			//攻击
			var propertyDetail:TextField = new TextField();
			propertyDetail.selectable = false;
        	propertyDetail.autoSize = TextFieldAutoSize.LEFT;
        	propertyDetail.multiline = true;
			propertyDetail.defaultTextFormat = new TextFormat("微软雅黑", 13, 0x000000, true);
			propertyDetail.htmlText = "攻击: <font color='#00FF00'>" + tankInfo.attackPoint + "</font>\t\t" + 
					"防御: <font color='#00FF00'>" + + tankInfo.armPoint + "</font>";
			propertyDetail.htmlText += "生命: <font color='#00FF00'>" + tankInfo.healthPoint + "</font>\t\t" + 
					"暴击: <font color='#00FF00'>" + + tankInfo.criticalAttackRatio + "</font>";
			propertyDetail.htmlText += "能量: <font color='#00FF00'>" + tankInfo.energyPoint + "</font>";
			propertyDetail.y = 50;
			roundRecContainer.addChild(propertyDetail);
			//横线
			roundRecContainer.graphics.lineStyle(1, 0, 0.5);
			roundRecContainer.graphics.moveTo(0, propertyDetail.height + 60);
			roundRecContainer.graphics.lineTo(240, propertyDetail.height + 60);
			//被动装备
			var passiveEquipMes:TextField = new TextField();
			passiveEquipMes.selectable = false;
        	passiveEquipMes.autoSize = TextFieldAutoSize.LEFT;
			passiveEquipMes.defaultTextFormat = new TextFormat("微软雅黑", 12, 0xFFFFFF);
			passiveEquipMes.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			passiveEquipMes.y = 130;
			var owner:PlayerInfo = PlayerInfoManager.getInstance().players[PlayerInfoManager.getInstance().myUserId];
			//增添装备
			var count:int = 0;
			for (var i:int = 0; i < 10; i++)
			{
				var comIndex:String = tankInfo.getEquipByPosition(i);
				if (comIndex != null)
				{
					var comIndexArr:Array = comIndex.split("_");
					var objectIndex:int = comIndexArr[0];
					var basicLevel:int = comIndexArr[1];
					var strengthenLevel:int = comIndexArr[2];
					var parseTankId:int = comIndexArr[3];
					if (parseTankId == tankId)
					{
						var equipConfig:EquipConfig = Config.objectConfigs[objectIndex];
						var equipMes:TextField = new TextField();
						equipMes.selectable = false;
			        	equipMes.autoSize = TextFieldAutoSize.LEFT;
						equipMes.defaultTextFormat = new TextFormat("微软雅黑", 13, Config.basicConfig.objectBasicLevelColor[basicLevel], true);
						equipMes.text = Config.basicConfig.equipSuitName[equipConfig.suitIndex] + "的" + equipConfig.name + " · "+ Config.basicConfig.objectBasicLevel[basicLevel] + Config.basicConfig.objectStrengthenLevel[strengthenLevel];
						equipMes.y = passiveEquipMes.y + 20 + count * 20;
						roundRecContainer.addChild(equipMes);
						count++;
					}
				}
			}
			passiveEquipMes.text = "装备 [" + count + "/" + tankInfo.getAvailableSlotNum() + "]";
			roundRecContainer.addChild(passiveEquipMes);
			//装备战力
			fpSprite = getFightPoint(tankInfo.fightPointEquip);
			fpSprite.x = 230 - fpSprite.width;
			fpSprite.y = passiveEquipMes.y;
			roundRecContainer.addChild(fpSprite);
			//横线
			roundRecContainer.graphics.lineStyle(1, 0, 0.5);
			roundRecContainer.graphics.moveTo(0, passiveEquipMes.y + 30 + count * 20);
			roundRecContainer.graphics.lineTo(240, passiveEquipMes.y + 30 + count * 20);
			//坦克
			var tankMes:TextField = new TextField();
			tankMes.selectable = false;
        	tankMes.autoSize = TextFieldAutoSize.LEFT;
			tankMes.defaultTextFormat = new TextFormat("微软雅黑", 12, 0xFFFFFF);
			tankMes.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			tankMes.text = "坦克";
			tankMes.y = passiveEquipMes.y + 40 + count * 20;
			//坦克战力
			fpSprite = getFightPoint(tankInfo.fightPointBase);
			fpSprite.x = 230 - fpSprite.width;
			fpSprite.y = tankMes.y;
			roundRecContainer.addChild(fpSprite);
			roundRecContainer.addChild(tankMes);
			//坦克品质
			var tankProperty:TextField = new TextField();
			tankProperty.selectable = false;
        	tankProperty.autoSize = TextFieldAutoSize.LEFT;
        	tankProperty.multiline = true;
			tankProperty.defaultTextFormat = new TextFormat("微软雅黑", 13, 0x000000, true);
			tankProperty.y = tankMes.y + 20;
    		tankProperty.htmlText = "品质: <font color='#"+ Config.basicConfig.tankBasicLevelColor[tankInfo.basicLevel].substr(2) + "'>" 
    			+ basicLevelName + strengthenLevelName + "</font>";
			roundRecContainer.addChild(tankProperty);
			//当前的底部位置
			var bottomY:int = tankProperty.y + tankProperty.height;
			var quality:int = tankInfo.basicLevel * 5 + tankInfo.strengthenLevel;
			if (quality < 24)
			{
				//横线
				roundRecContainer.graphics.lineStyle(1, 0, 0.5);
				roundRecContainer.graphics.moveTo(0, tankProperty.y + 30);
				roundRecContainer.graphics.lineTo(240, tankProperty.y + 30);
				var nextLevelContainer:Sprite = new Sprite();
				//下一级
				var nextBasicLevel:int = (quality + 1) / 5;
				var nextStrengthenLevel:int = (quality + 1) % 5;
				var nextLevelText:TextField = new TextField();
				nextLevelText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
				nextLevelText.selectable = false;
	        	nextLevelText.autoSize = TextFieldAutoSize.LEFT;
				nextLevelText.defaultTextFormat = new TextFormat("微软雅黑", 12, 0xFFFFFF, true);
				nextLevelText.text = "下一级坦克";
				nextLevelText.y = tankProperty.y + 40;
				nextLevelContainer.addChild(nextLevelText);
				fpSprite = getFightPoint(tankConfig.fightPoint[quality + 1]);
				fpSprite.x = 230 - fpSprite.width;
				fpSprite.y = nextLevelText.y;
				nextLevelContainer.addChild(fpSprite);
				//下一级效果
				var nextTipText:TextField = new TextField();
				nextTipText.selectable = false;
	        	nextTipText.autoSize = TextFieldAutoSize.LEFT;
	        	nextTipText.width = 180;
	        	nextTipText.wordWrap = true;
				nextTipText.multiline = true;
				nextTipText.defaultTextFormat = new TextFormat("微软雅黑", 13, 0x000000, true);
				nextTipText.htmlText = "品质: <font color='#"+ Config.basicConfig.tankBasicLevelColor[nextBasicLevel].substr(2) + "'>" + 
					Config.basicConfig.tankBasicLevel[nextBasicLevel] + Config.basicConfig.tankStrengthenLevel[nextStrengthenLevel] + "</font>";
				nextTipText.y = nextLevelText.y + 20;
				nextLevelContainer.addChild(nextTipText);
				nextLevelContainer.x = 10;
				roundRecContainer.addChild(nextLevelContainer);
				bottomY = nextTipText.y + nextTipText.height;
			}
			//仓库中的非出战坦克才可以操作
			var me:PlayerInfo = PlayerInfoManager.getInstance().me;
			if (tankInfo.ownerId == me.id && tankId != me.campTankId)
			{ 
				//横线
				roundRecContainer.graphics.lineStyle(1, 0, 0.5);
				roundRecContainer.graphics.moveTo(0, bottomY + 10);
				roundRecContainer.graphics.lineTo(240, bottomY + 10);
				//售出
				var sellText:TextField = new TextField();
				sellText.selectable = false;
	        	sellText.autoSize = TextFieldAutoSize.LEFT;
				sellText.defaultTextFormat = new TextFormat("微软雅黑", 13, 0x0000FF, true, null, true);
				sellText.text = "售出";
				sellText.x = 10;
				sellText.y = bottomY + 20;
				sellText.addEventListener(MouseEvent.MOUSE_OVER, onTextMouseOver);
				sellText.addEventListener(MouseEvent.MOUSE_OUT, onTextMouseOut);
				sellText.addEventListener(MouseEvent.CLICK, onSellTextClick);
				roundRecContainer.addChild(sellText);
			}

			roundRecContainer.x = 10;
			roundRecContainer.y = 10;
			var roundRec:RoundRec = new RoundRec(260, roundRecContainer.height + 30, 0.8, BFont.roundRecColor);
			addChild(roundRec);
			addChild(roundRecContainer);
			this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
		}
		
		private function onTextMouseOver(event:MouseEvent):void
		{
			Mouse.cursor = "button";
		}
		
		private function onTextMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
		}
		
		private function onSellTextClick(event:MouseEvent):void
		{
			if (TechTower.getInstance().parent != null)
			{
	        	TechTower.getInstance().removeAll();
	        	Warehouse.getInstance().refresh();
	  		}
	  		var tankInfo:TankInfo = PlayerInfoManager.getInstance().tanks[tankId];
			var purchaseDialog:PurchaseDialog = new PurchaseDialog(tankId, tankInfo.index, tankInfo.basicLevel, tankInfo.strengthenLevel, 1);
			WidgetManager.getInstance().popOut([purchaseDialog], true, true);
			//关闭
			WidgetManager.getInstance().closeMouseOverMessage();
		}
		
		private function getFightPoint(fp:int):Sprite
		{
			var fpContainer:Sprite = new Sprite();
			//战力图标
			var CampIcon:Class = ResourceHelper.getInstance().getClassByName("CampIcon");
			var fpIcon:Sprite = new CampIcon();
			fpIcon.width = 20;
			fpIcon.height = 20;
			fpContainer.addChild(fpIcon);
			//坦克战力
			var fightPoint:TextField = new TextField();
			fightPoint.selectable = false;
        	fightPoint.autoSize = TextFieldAutoSize.LEFT;
			fightPoint.defaultTextFormat = new TextFormat("微软雅黑", 12, 0xFFFFFF);
			fightPoint.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			fightPoint.text = fp.toString();
			fightPoint.x = 25;
			fpContainer.addChild(fightPoint);
			return fpContainer;
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			if (event.stageX < this.x || event.stageX > this.x + this.width || 
				event.stageY < this.y || event.stageY > this.y + this.height)
			{ 
				WidgetManager.getInstance().closeMouseOverMessage();
			}
		}
		
	}
}